package com.kouxin.appaccount.software;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.kouxin.appaccount.developer.Developer;
import com.kouxin.appaccount.developer.DeveloperAuthenticationInterceptor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SoftwareAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = -3629501051990168346L;

	private Software software;
	private SoftwareDao softwareDao;
	private Map<String, Object> session;

	public String newapp() throws Exception {
		Developer developer = (Developer) session.get(DeveloperAuthenticationInterceptor.DEVELOPER_SESSION_KEY);
		software.setDeveloperId(developer.getId());
		softwareDao.insert(software);
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String editapp() throws Exception {
		session = (Map<String, Object>) ActionContext.getContext().get(ActionContext.SESSION);
		Developer developer = (Developer) session.get(DeveloperAuthenticationInterceptor.DEVELOPER_SESSION_KEY);
		HttpServletRequest request = ServletActionContext.getRequest();
		software = softwareDao.get(request.getParameter("software.id"));
		if (software == null || !developer.getId().equals(software.getDeveloperId())) {
			this.addActionError("找不到要编辑的软件");
			request.setAttribute("redirect", request.getContextPath() + "/developer/applist");
			return ERROR;
		} else {
			return INPUT;
		}
	}
	
	@SuppressWarnings("unchecked")
	public String saveapp() throws Exception {
		session = (Map<String, Object>) ActionContext.getContext().get(ActionContext.SESSION);
		Developer developer = (Developer) session.get(DeveloperAuthenticationInterceptor.DEVELOPER_SESSION_KEY);
		HttpServletRequest request = ServletActionContext.getRequest();
		Software software = softwareDao.get(this.software.getId());
		if (software == null || !developer.getId().equals(software.getDeveloperId())) {
			this.addActionError("找不到要保存的软件");
			request.setAttribute("redirect", request.getContextPath() + "/developer/applist");
			return ERROR;
		} else {
			software.setName(this.software.getName());
			software.setDescription(this.software.getDescription());
			if (software.getPricingStrategyType() == null) software.setPricingStrategyType(this.software.getPricingStrategyType());
			softwareDao.update(software);
			return SUCCESS;
		}
	}

	@SuppressWarnings("unchecked")
	public String delapp() throws Exception {
		session = (Map<String, Object>) ActionContext.getContext().get(ActionContext.SESSION);
		Developer developer = (Developer) session.get(DeveloperAuthenticationInterceptor.DEVELOPER_SESSION_KEY);
		HttpServletRequest request = ServletActionContext.getRequest();
		software = softwareDao.get(request.getParameter("software.id"));
		if (software == null || !developer.getId().equals(software.getDeveloperId())) {
			this.addActionError("找不到要删除的软件");
			request.setAttribute("redirect", request.getContextPath() + "/developer/applist");
			return ERROR;
		} else {
			softwareDao.delete(software);
			this.addActionMessage("操作成功");
			request.setAttribute("redirect", request.getContextPath() + "/developer/applist");
			return SUCCESS;
		}
	}
	
	public Software getSoftware() {
		return software;
	}

	public void setSoftware(Software software) {
		this.software = software;
	}

	public SoftwareDao getSoftwareDao() {
		return softwareDao;
	}

	public void setSoftwareDao(SoftwareDao softwareDao) {
		this.softwareDao = softwareDao;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

}
